//////////////////////////////////////////////////////////////////////////////
//    Copyright 2004, SenseGraphics AB
//
//    This file is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    H3D API is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with H3D API; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//    A commercial license is also available. Please contact us at 
//    www.sensegraphics.com for more information.
//
//
/// \file Torus.h
/// \brief Header file for Torus, X3D scene-graph node
///
//
//////////////////////////////////////////////////////////////////////////////
#ifndef __TORUS_H__
#define __TORUS_H__

#include "UsefulNodes.h"
#include <H3D/X3DGeometryNode.h>
#include <H3D/SFInt32.h>

namespace H3D {

  /// \ingroup Geometries
  /// \class Torus
  /// \brief The Torus node defines a torus geometry.
  ///
  /// The innerRadius and outerRadius fields define the inner and outer radius
  /// of the torus.
  /// 
  /// The nrSides and nrRings define the resolution of the tesselation. nrSides
  /// is the number of sides for each radial section and nrRings is the number
  /// of radial divisions of the torus.
  ///
  /// The solid field determines whether the Torus is visible when viewed from
  /// the inside. 
  ///
  /// \par Internal routes:
  /// \dotfile Torus.dot
  class Torus : public X3DGeometryNode {
  public:

    /// SFBound is specialized update itself from the innerRadius and
    /// outerRadius field of the Torus node.
    ///
    /// routes_in[0] is the innerRadius field of the Torus node.  
    /// routes_in[1] is the outerRadius field of the Torus node.  
    
    class SFBound: public TypedField< X3DGeometryNode::SFBound,
                                      Types< SFFloat, SFFloat > > {
      /// Update the bound from the radius field. 
      virtual void update() {
        H3DFloat inner_radius = 
          static_cast< SFFloat * >( routes_in[0] )->getValue();
        H3DFloat outer_radius = 
          static_cast< SFFloat * >( routes_in[1] )->getValue();
        BoxBound *bb = new BoxBound;
        bb->size->setValue( Vec3f( (outer_radius+inner_radius)*2, 
                                   (outer_radius+inner_radius)*2,
                                   inner_radius * 2 ) );
        value = bb;
      }
    };
    
    /// Constructor.
    Torus( Inst< SFNode      > _metadata        = 0,
           Inst< SFBound     > _bound           = 0,
	   Inst< DisplayList > _displayList     = 0,
	   Inst< MFBool      > _isTouched       = 0,
	   Inst< MFVec3f     > _force           = 0,
	   Inst< MFVec3f     > _contactPoint    = 0,
	   Inst< MFVec3f     > _contactNormal   = 0,
	   Inst< MFVec3f     > _contactTexCoord = 0,
	   Inst< SFBoundTree > _boundTree       = 0,
           Inst< SFFloat     > _innerRadius     = 0,
           Inst< SFFloat     > _outerRadius     = 0,
           Inst< SFInt32     > _nrSides         = 0,
           Inst< SFInt32     > _nrRings         = 0,
           Inst< SFBool      > _solid           = 0 );

    /// Renders the Torus using OpenGL.
    virtual void render();

    /// Traverse the scenegraph. 
    virtual void traverseSG( TraverseInfo &ti );  

    /// The innerRadius field define the inner radius of the torus. 
    ///
    /// <b>Access type:</b> inputOutput
    /// <b> Default value:</b> 0.5
    auto_ptr< SFFloat >  innerRadius;

    /// The outerRadius field define the inner radius of the torus. 
    ///
    /// <b>Access type:</b> inputOutput
    /// <b> Default value:</b> 1
    auto_ptr< SFFloat >  outerRadius;

    /// The nrSides field define the number of sides for each radial section
    /// of the torus.
    ///
    /// <b>Access type:</b> inputOutput
    /// <b> Default value:</b> 0.5
    auto_ptr< SFInt32 >  nrSides;

    /// The nrRings field define the number of radial divisions of the 
    /// torus.
    ///
    /// <b>Access type:</b> inputOutput
    /// <b> Default value:</b> 0.5
    auto_ptr< SFInt32 >  nrRings;

    /// The solid field determines whether the Torus is visible when viewed from
    /// the inside. 
    /// <b>Access type:</b>  inputOutput
    auto_ptr< SFBool  >  solid;

    /// The H3DNodeDatabase for this node.
    static H3DNodeDatabase database;
  };
}

#endif
